import Vue from "vue";
import axios from "axios";
import VueAxios from "vue-axios";
import tokenUtil from "@/core/util/tokenUtil.js";
import Swal from "sweetalert2";
import Router from "@/router.js";
import {handleNotify,handleAlert,showWating,closeWating} from "@/core/util/jehcUtil.js";

const service = axios.create({
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 5000 // request timeout
})
// this.setHeader();//设置请求前增加header
//请求数据拦截器
service.interceptors.request.use(request => {
    let token = `${tokenUtil.getToken()}`;
    let JEHC_SESSION_ID = `${tokenUtil.getJSessionId()}`;
    if(token===undefined ||token==="undefined"){
      token = "";
    }    
    if(JEHC_SESSION_ID===undefined||JEHC_SESSION_ID==='undefined'||JEHC_SESSION_ID==="null"||JEHC_SESSION_ID===null){
      JEHC_SESSION_ID = "";
    }    
    request.headers["token"] = token;
    request.headers["JEHC-SESSION-ID"] = JEHC_SESSION_ID;
    //全局显示加载进度条
    // showWating({});
    return request
    }, err => {
        return Promise.reject(err);
    });    
    
    //接收响应拦截器
    service.interceptors.response.use(response => {
    // console.log("拦截器结果：",response.data.status);
    // handleNotify ('通知', '您的账号由于长时间没有操作已经失效', 'warning', 'top-right',  '3000');
    let requestStatus = response.status;
    if(requestStatus == 200){//请求状态码成功
        let status = response.data.status;        
        closeWating();//关闭进度条
        //1.优先级判断访问成功后是否存在自定义非法信息提示
        if(status !== undefined){
            if(status==888){         
                tokenUtil.destroyToken();
                //1.session失效
                handleNotify ('通知', '您的账号由于长时间没有操作已经失效', 'warning', 'top-right',  '3000');
                setTimeout(function () {         
                // Router.push({ path: "/login" })
                Router.push("/login" );
                // this.$router.push('/login');
                }, 2000);
                return Promise.reject("");
            }else if(status==777){
                //2.功能权限
                handleAlert("暂无权限", "warning", 3)
                return Promise.reject("");
            }else if(status==999){
                //3.平台密钥不存在
                handleAlert("平台密钥不存在", "warning", 3)
                return Promise.reject("");
            }else if(status==666){
                //4.非法页面
                handleAlert("您的请求访问，已经列入到黑名单中，建议您联系管理员，谢谢！", "warning", 3)
                return Promise.reject("");
            }else if(status == 500){
                Swal.fire({
                    text:response.data.message,
                    icon: "error",
                    buttonsStyling: false,
                    confirmButtonText: "请重新试一下!",
                    customClass: {
                        confirmButton: "btn fw-bold btn-light-danger",
                    }
                });
                return Promise.reject("");
            }
        }
    }    
        return response
    }, err => {    
        let error = {};  
        closeWating();//关闭进度条
        if (err && err.response) {
            switch (err.response.status) {
                case 400: error.message = '请求错误(400)'; break;
                case 401: Router.push("/login" ); break;
                case 403: error.message = '拒绝访问(403)'; break;
                case 404: error.message = '请求出错(404)'; break;
                case 408: error.message = '请求超时(408)'; break;
                case 500: error.message = '服务器错误(500)'; break;
                case 501: error.message = '服务未实现(501)'; break;
                case 502: error.message = '网络错误(502)'; break;
                case 503: error.message = '服务不可用(503)'; break;
                case 504: error.message = '网络超时(504)'; break;
                case 505: error.message = 'HTTP版本不受支持(505)'; break;
                default: error.message = `连接出错(${err.response.status})!`;
            }
        } else {
            error.message = '连接服务器失败!'
        }   
        handleAlert(error.message, "error", 3)
        return Promise.reject(err);
    });

export default service
